﻿using System;
using System.Collections.Generic;
using System.IO;
using Citi.GocAttributes.Domain.Utils;
using Citi.Import.Util;

namespace Citi.GocAttributes.Domain
{
    public partial class ImportHistory
    {
        public static ImportHistory SaveHistory(IEnumerable<Error> errors, string fileName)
        {
            var importHistory = new ImportHistory
                {
                    FileName = Path.GetFileName(fileName),
                    UploadDate = DateTime.Now
                };

            foreach (var error in errors)
            {
                importHistory.ImportErrors.Add(new ImportError
                    {
                        Error = error.ErrorDescription,
                        Line = error.Line,
                        Fields = error.Fields != null ? string.Join("|", error.Fields) : null
                    });
            }

            Context.ClearContext();
            importHistory.Save(importHistory);

            return importHistory;
        }
    }
}